package com.jcl.recruit.console.db.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jcl.core.common.page.PageResult;
import com.jcl.core.constants.Constant;
import com.jcl.core.utils.StringUtils;
import com.jcl.recruit.console.db.logic.DbCorpLogic;
import com.jcl.recruit.console.db.logic.DbServerLogic;
import com.jcl.recruit.console.model.ConsoleDbServer;
import com.jcl.recruit.console.vo.ConsoleDbServerVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static org.springframework.beans.support.PagedListHolder.DEFAULT_PAGE_SIZE;

/**
 * @ClassName DbServerService
 * @Author yyliu
 * @Date 2018/12/21 15:11
 * @Version 1.0
 **/
@Service
public class DbServerService {

    @Autowired
    private DbCorpLogic dbCorpLogic;

    @Autowired
    private DbServerLogic dbServerLogic;

    public PageResult pageList(Integer curPage, Integer rowSize) {
        curPage = null == curPage ? 1 : curPage;
        rowSize = null == rowSize ? DEFAULT_PAGE_SIZE : rowSize;
        PageHelper.startPage(curPage, rowSize);
        List<ConsoleDbServer> serverList = dbServerLogic.findList();
        PageInfo info = new PageInfo(serverList);
        List<ConsoleDbServerVo> voList = dbServerLogic.serverList(info.getList());
        return new PageResult(Integer.valueOf(curPage), rowSize,
                info.getTotal(), voList);
    }

    public ConsoleDbServerVo getOne(Integer id) {
        ConsoleDbServer server = dbServerLogic.findOne(id);
        if(null != server){
            ConsoleDbServerVo serverVo = new ConsoleDbServerVo();
            BeanUtils.copyProperties(server, serverVo);
            serverVo.setDbPassword(null);
            return serverVo;
        }
        return null;
    }

    public Map<String, Integer> prefixMap() {
        Map<String, Integer> prefixMap = dbServerLogic.prefixMap();
        return prefixMap;
    }

    public int updateConsoleDbServer(Integer id, Integer corpNumber) {

        ConsoleDbServer server = dbServerLogic.findOne(id);
        if (null == server) {
            // 不存在
            return 0;
        }
        int userTotal = server.getCorpEnd() - server.getCorpStart() + 1;
        int lastTotal = server.getTotal() - userTotal;

        if (lastTotal < corpNumber) {
            corpNumber = lastTotal;
        }
        int start = server.getCorpEnd() + 1;
        server.setCorpEnd(server.getCorpEnd() + corpNumber);
        dbServerLogic.update(server);
        // 初始化 企业corpCode
        dbCorpLogic.initDbCorp(server.getServerName(), server.getPrefix(),
                start, server.getCorpEnd());
        return 1;
    }

    public int addConsoleDbServer(ConsoleDbServerVo vo) {

        ConsoleDbServer server = dbServerLogic.findOneByServerName(vo.getServerName());
        if (null != server) {
            // 已存在不能新增
            return 1;
        }
        String prefix = vo.getPrefix();
        if (StringUtils.isEmpty(vo.getPrefix())) {
            prefix = Constant.CORP_CODE_PREFIX;
        }
        if (null == vo.getCorpNumber()) {
            vo.setCorpNumber(Constant.CORP_TOTAL);
        }
        int start = 0;
        if (null != prefix) {
            if (Constant.CORP_CODE_PREFIX.equals(prefix)) {
                List<ConsoleDbServer> serverList = dbServerLogic.findListByPrefix(prefix);
                for(ConsoleDbServer dbServer: serverList){
                    if(!dbServer.getServerName().equals(Constant.ELASTIC_DB_NAME)){
                        start += Constant.CORP_TOTAL * serverList.size();
                    }
                }
            }
        }
        start++;
        vo.setCorpStart(start);

        if (null == server) {
            server = dbServerLogic.add(vo);
        }
        // 初始化 企业corpCode
        dbCorpLogic.initDbCorp(server.getServerName(), server.getPrefix(),
                server.getCorpStart(), server.getCorpEnd());
        return 0;
    }

}
